////
//
/// \file ./src/variable_decl_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/variable_decl_node.hpp>

#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/ws_parser.hpp>
#include <inc/parser/parse_tree_node.hpp>
#include <inc/parser/label_node.hpp>
#include <inc/parser/type_node.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::scoped_ptr;

/// \brief Basic constructor for a variable declaration node
/// \author Chandler Carruth
/// \date 2008.10.04

VariableDeclNode::VariableDeclNode()
    : ws(new WSParser)
{}

/// \brief Empty destructor, all members are smart pointers
/// \author Chandler Carruth
/// \date 2008.10.04

VariableDeclNode::~VariableDeclNode()
{}

/// \brief Parse a variable declaration in INC
/// \author Chandler Carruth
/// \date 2008.10.04
///
/// Parse the declaration of a variable. No assumptions are made about context,
/// it simply associates a label with a type.
///
/// Example: 'let x be float< 64 >'

bool
VariableDeclNode::parse( const iterator &first,
                         const iterator &last,
                         iterator &last_parsed )
{
  assert( get_text().empty() && !type && !label
          && "This node was parsed more than once!" );

  scoped_ptr<LabelNode> tmp_label( new LabelNode );
  if( tmp_label->parse( first, last, last_parsed ) )
  {
    iterator current = last_parsed;
    if( ws->parse_ws( current, last, last_parsed ) )
    {
      current = last_parsed;
      if( std::distance( current, last ) >= 2
          && string( current, current + 2 ) == "is" )
      {
        current += 2;
        if( ws->parse_ws( current, last, last_parsed ) )
        {
          current = last_parsed;

          scoped_ptr<TypeNode> tmp_type( new TypeNode );
          if( tmp_type->parse( current, last, last_parsed ) )
          {
            set_text( string( first, last_parsed ) );
            label.swap( tmp_label );
            type.swap( tmp_type );
            return true;
          }
        }
      }
    }
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief Convenience getter for the label
/// \author Chandler Carruth
/// \date 2008.10.04

LabelNode*
VariableDeclNode::get_label() const
{
  return label.get();
}

/// \brief Convenience getter for the type
/// \author Chandler Carruth
/// \date 2008.10.04

TypeNode*
VariableDeclNode::get_type() const
{
  return type.get();
}

} //  end parser namespace
} //  end inc namespace
